package com.fenger.listener;

import com.fenger.servlet.MyServlet;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
 * @PackageName: com.fenger.linstener
 * @ClassName: MyLinstener
 * @Description:
 * @author: fang
 * @date: 2021/11/1 7:54
 */
@WebListener
public class MyListener implements HttpSessionListener, ServletContextListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("session be created id:" + session.getId());
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        System.out.println("session be destroyed id:" + session.getId());
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();
        try {
            MyServlet myServlet = servletContext.createServlet(MyServlet.class);
            ServletRegistration.Dynamic my = servletContext.addServlet("my", myServlet);
        } catch (ServletException e) {
            e.printStackTrace();
        }
        System.out.println("add servlet success");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        Object source = sce.getSource();
        System.out.println("context be destroyed id:" + source);
    }
}
